package ruru.client.backend;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RuruUtility {
	private static final long SECOND = 1000;
	private static final long MINUTE = SECOND * 60;
	private static final long HOUR = MINUTE * 60;
	private static final long DAY = HOUR * 24;
	private static final long WEEK = DAY * 7;
	private static NumberFormat nf = NumberFormat.getInstance();
	
	// public static DateTimeFormat dtf = DateTimeFormat.getFormat("hh:mma EEE MM-dd-yyyy");

	public static String lastUpdate(long date) {
		long current = new Date().getTime();
		long diff = current - date;
		String time = "uninitialized :P";

		if(diff < MINUTE) {
			time = (diff / SECOND) + ((diff / SECOND == 1) ? " second" : " seconds");
		} else if(diff < HOUR) {
			time = (diff / MINUTE) + ((diff / MINUTE == 1) ? " minute" : " minutes");
		} else if(diff < DAY) {
			time = (diff / HOUR) + ((diff / HOUR == 1) ? " hour" : " hours");
		} else if(diff < WEEK) {
			time = (diff / DAY) + ((diff / DAY == 1) ? " day" : " days");
		} else {
			time = (diff / WEEK) + ((diff / WEEK == 1) ? " week" : " weeks");
		}

		return time;
	}

	public static String twoDecimals(double value) {
		nf.setFixedPrecision(2);
		return nf.format(new Double(value).toString());
	}
	
	public static String convertToNBSP(String substrate) {
		StringBuffer result = new StringBuffer();
		
		for(int i = 0; i < substrate.length(); i++) {
			if(substrate.charAt(i) == ' ') {
				result.append("&nbsp;");
			} else {
				result.append(substrate.charAt(i));
			}
		}
		
		return result.toString();
	}

	/**
	 * Converts spaces in this ItemLink's name to "+" to satisfy the Allakhazam API.
	 * 
	 * @param name
	 * @return
	 */
	public static String convertSpaces(String name) {
		StringBuffer result = new StringBuffer();

		for(int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);

			if(c == ' ') {
				result.append("+");
			} else {
				result.append(c);
			}
		}
		return result.toString();
	}

	/**
	 * Recursively removes all starting spaces from target. For example, " Sonari's Mom" returns "Sonari's Mom".
	 * 
	 * @param target
	 * @return
	 */
	public static String removeStartingSpaces(String target) {
		char startChar = target.charAt(0);
		if(startChar == ' ') {
			return removeStartingSpaces(target.substring(1));
		} else
			return target;
	}

	/**
	 * Tokenizes a String by a space delimiter.
	 * 
	 * @param target
	 * @return
	 */
	public static List tokenizer(String target) {
		// List result = new ArrayList();
		//		
		// StringBuffer token = new StringBuffer();
		//
		// for(int i = 0; i < target.length(); i++) {
		// if(target.charAt(i) == ' ') {
		// result.add(token.toString());
		// token = new StringBuffer();
		// } else {
		// token.append(target.charAt(i));
		// }
		// }
		//
		// result.add(token.toString());
		List result = new ArrayList();
		String[] tokenized = target.split("[ ]");
		for(int i = 0; i < tokenized.length; i++) {
			result.add(tokenized[i]);
		}

		return result;
	}

	/**
	 * Capitalizes the first letter of each word in target, concatenates them with spaces. Doesn't capitalized "of" and
	 * "the", and capitalizes all roman numerals. Assumes target contains at least one string.
	 * 
	 * @param target
	 * @return
	 */
	public static String capitalize(List target) {
		List romanNumerals = new ArrayList();
		romanNumerals.add("i");
		romanNumerals.add("ii");
		romanNumerals.add("iii");
		romanNumerals.add("iv");
		romanNumerals.add("v");
		romanNumerals.add("vi");
		romanNumerals.add("vii");
		romanNumerals.add("viii");
		romanNumerals.add("ix");
		romanNumerals.add("x");
		romanNumerals.add("xi");
		romanNumerals.add("xii");
		romanNumerals.add("xiii");
		romanNumerals.add("xiv");
		romanNumerals.add("xv");
		romanNumerals.add("xvi");
		romanNumerals.add("xvii");
		romanNumerals.add("xviii");
		romanNumerals.add("xix");
		romanNumerals.add("xx");

		List noCaps = new ArrayList();
		noCaps.add("of");
		noCaps.add("the");

		StringBuffer buffer = new StringBuffer();
		String first = (String) target.get(0);

		// always capitalize first character
		buffer.append(capFirstLetter(first));

		if(target.size() > 1) {
			for(int i = 1; i < target.size(); i++) {
				buffer.append(" ");

				String s = (String) target.get(i);
				if(romanNumerals.contains(s)) {
					buffer.append(capAll(s));
				} else if(noCaps.contains(s)) {
					buffer.append(s);
				} else {
					buffer.append(capFirstLetter(s));
				}

			}
		}

		return buffer.toString();
	}

	/**
	 * Capitalizes first letter, also caps any letters following a "-". Bloodmaw Magus-Blade ftl.
	 * 
	 * @param s
	 * @return
	 */
	public static String capFirstLetter(String s) {
		StringBuffer buffer = new StringBuffer();
		boolean minusWasLastChar = false;

		for(int i = 0; i < s.length(); i++) {
			Character c = new Character(s.charAt(i));

			if(i == 0 || minusWasLastChar) {
				buffer.append(Character.toUpperCase(c.charValue()));
			} else
				buffer.append(c);

			if(s.charAt(i) == '-') {
				minusWasLastChar = true;
			} else
				minusWasLastChar = false;
		}

		return buffer.toString();
	}
	
	/**
	 * Warning: is not a complete inverse of capFirstLetter. Doesn't deal with "-" as in Bloodmaw Magus-Blade.
	 * @param s
	 * @return
	 */
	public static String lowerFirstLetter(String s) {
		StringBuffer buffer = new StringBuffer();
		for(int i = 0; i < s.length(); i++) {
			Character c = new Character(s.charAt(i));

			if(i == 0) {
				buffer.append(Character.toLowerCase(c.charValue()));
			} else {
				buffer.append(c);
			}
		}
		return buffer.toString();
	}

	public static String capAll(String s) {
		StringBuffer buffer = new StringBuffer();

		for(int i = 0; i < s.length(); i++) {
			buffer.append(Character.toUpperCase(s.charAt(i)));
		}

		return buffer.toString();
	}
}